package com.sgb.member.ui.activity


import android.app.Activity
import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.tabs.TabLayout
import com.sgb.member.R
import com.sgb.member.databinding.ActivityMemberDoorBinding
import com.sgb.member.entity.Record
import com.sgb.member.ui.adapter.MemberDoorAdapter2
import com.sgb.member.view_model.MemberDoorModel
import com.swgk.core.base.AppActivity
import com.swgk.core.customview.XCustomTabLayout.SimpleSelectedListener
import com.swgk.core.dialog.DialogHelper
import com.swgk.core.util.MToast
import com.swgk.core.view.selection.CommonPopEntity
import com.swgk.core.xrecyclerview.XRecyclerView
import java.util.*


class MemberDoorActivity : AppActivity() {
    private lateinit var viewModel: MemberDoorModel
    private lateinit var mBinding: ActivityMemberDoorBinding

    var page = 1
    var mData: ArrayList<Record> = ArrayList()

    companion object {
        fun jumptToAct(activity: Activity) {
            val intent = Intent(activity, MemberDoorActivity::class.java)
            activity.startActivity(intent)
        }
    }

    var tab1Data: MutableList<CommonPopEntity> = ArrayList()
    var tab2Data: MutableList<CommonPopEntity> = ArrayList()
    var tab3Data: MutableList<CommonPopEntity> = ArrayList()
    var tab4Data: MutableList<CommonPopEntity> = ArrayList()

    lateinit var  adapter: MemberDoorAdapter2

    var mIsLoadMore = false
    var relation = ""
    var firstCategory = ""
    var secondCategory = ""
    var thirdCategory = ""

    var keyword = ""
    override fun init() {
        super.init()
        viewModel.getTab1Data()
    }

    override fun initView() {
        super.initView()
        viewModel = ViewModelProvider(this).get(MemberDoorModel::class.java)
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_member_door)
        mBinding.lifecycleOwner = this
        mBinding.activityBar.ivBack.setOnClickListener { finish() }
        mBinding.activityBar.tvSearch.text = "新增"
        mBinding.activityBar.etView.hint="请输入要求内容"
        mBinding.activityBar.tvSearch.setOnClickListener { AddMemberDoorActivity.start(this) }

        initEdit()
        receiverLiveData()
        initTabLayout()
        initRecycleView()
    }

    override fun onRestart() {
        super.onRestart()
        loadData(false)
    }

    fun initRecycleView() {
        adapter = MemberDoorAdapter2(mData)
        adapter.setOnItemClickListener { it:Record, typeName:String->
            AddMemberDoorActivity.start(this,it.relation,it.firstCategory,it.secondCategory,it.thirdCategory,typeName)
        }

        mBinding.recyclerView.adapter = adapter
        mBinding.recyclerView.setLoadingMoreEnabled(true)
        mBinding.recyclerView.setLoadingListener(object : XRecyclerView.LoadingListener {
            override fun onRefresh() {
                loadData(false)
            }

            override fun onLoadMore() {
                loadData(true)
            }
        })
    }

    fun initTabLayout() {
        mBinding.tabLayout1.addOnTabSelectedListener(tab1Data, object : SimpleSelectedListener() {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                val pos = tab!!.position
                relation = tab1Data[pos].no
                viewModel.getTab2Data(relation)
            }
        })
        mBinding.tabLayout2.addOnTabSelectedListener(tab2Data, object : SimpleSelectedListener() {
            override fun onTabSelected(tab: TabLayout.Tab) {
                val pos = tab.position
                firstCategory = tab2Data[pos].no
                getTab3Data(tab2Data[pos].children)
            }
        })
        mBinding.tabLayout3.addOnTabSelectedListener(tab3Data, object : SimpleSelectedListener() {
            override fun onTabSelected(tab: TabLayout.Tab) {
                val pos = tab.position
                secondCategory = tab3Data[pos].no
                getTab4Data(tab3Data[pos].children)
            }
        })
        mBinding.tabLayout4.addOnTabSelectedListener(tab4Data, object : SimpleSelectedListener() {
            override fun onTabSelected(tab: TabLayout.Tab) {
                val pos = tab.position
                thirdCategory = tab4Data[pos].no
                loadData(false)
            }
        })
    }

    fun initEdit() {
        mBinding.activityBar.etView.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}

            override fun afterTextChanged(s: Editable) {
                keyword = s.toString()
                loadData(false)
            }
        })
        mBinding.activityBar.tvSearchDelete.setOnClickListener {
            mBinding.activityBar.etView.setText("")
            loadData(false)
        }
    }

    private fun receiverLiveData() {
        viewModel.isLoadLiveData.observe(this, { t: Boolean ->
            if (t) {
                DialogHelper.showProgressDialog(this, "数据获取中...")
                    .setCanceledOnTouchOutside(false)
            } else DialogHelper.dismissProgressDialog()
        })
        viewModel.msgLiveData.observe(this, { t ->
            MToast.showLongToast(this, t)
        })
        viewModel.tab1LiveData.observe(this, { t: List<CommonPopEntity> ->
            tab1Data = t as MutableList<CommonPopEntity>
            for (data in t) {
                mBinding.tabLayout1.addTab(
                    mBinding.tabLayout1.newTab().setText(data.pickerViewText)
                )
            }
        })
        viewModel.tab2LiveData.observe(this, { t: List<CommonPopEntity> ->
            mBinding.tabLayout2.removeAllTabs()
            tab2Data = t as MutableList<CommonPopEntity>
            for (data in t) {
                mBinding.tabLayout2.addTab(
                    mBinding.tabLayout2.newTab().setText(data.pickerViewText)
                )
            }
        })
        viewModel.listLiveData.observe(this, { t ->
            if (mIsLoadMore) {
                if (null != t && t.size > 0) {
                    mBinding.recyclerView.loadMoreComplete()
                    mData.addAll(t)
                    adapter.notifyDataSetChanged()
                } else {
                    mBinding.recyclerView.setNoMore(true)
                }
                mBinding.recyclerView.visibility = View.VISIBLE
            } else {
                mBinding.recyclerView.refreshComplete()
                if (null != t && t.size > 0) {
                    mBinding.recyclerView.visibility = View.VISIBLE
                    mBinding.emptyView.visibility = View.GONE
                    mData.clear()
                    mData.addAll(t)
                    adapter.notifyDataSetChanged()
                    mBinding.recyclerView.setLoadingMoreEnabled(true)
                } else {
                    mData.clear();
                    adapter.notifyDataSetChanged()
                    mBinding.emptyView.visibility = View.VISIBLE
                }
            }
        })
    }

    fun getTab3Data(datas: List<CommonPopEntity>?) {
        tab3Data.clear()
        tab3Data.add(CommonPopEntity("", "不限"))
        if (datas != null) {
            if (datas.isNotEmpty()) {
                tab3Data.addAll(datas)
                mBinding.tabLayout3.visibility = View.VISIBLE
            } else mBinding.tabLayout3.visibility = View.GONE
        } else {
            mBinding.tabLayout3.visibility = View.GONE
        }
        mBinding.tabLayout3.removeAllTabs()
        for (data in tab3Data) {
            mBinding.tabLayout3.addTab(mBinding.tabLayout3.newTab().setText(data.pickerViewText))
        }
    }

    fun getTab4Data(datas: List<CommonPopEntity>?) {
        tab4Data.clear()
        tab4Data.add(CommonPopEntity("", "不限"))
        if (datas != null) {
            if (datas.isNotEmpty()) {
                tab4Data.addAll(datas)
                mBinding.tabLayout4.visibility = View.VISIBLE
            } else mBinding.tabLayout4.visibility = View.GONE
        } else {
            mBinding.tabLayout4.visibility = View.GONE
        }
        mBinding.tabLayout4.removeAllTabs()
        for (data in tab4Data) {
            mBinding.tabLayout4.addTab(mBinding.tabLayout4.newTab().setText(data.pickerViewText))
        }
    }

    fun loadData(isLoadMore: Boolean) {
        if(!isLoadMore){
            page=1
        }else{
            page++
        }
        mIsLoadMore = isLoadMore
//        Log.i("xxx","firstCategory->$firstCategory secondCategory-> $secondCategory thirdCategory-> $thirdCategory")
//        Log.i("xxx","relation-> $relation keyword $keyword page ->$page")
        viewModel.getListData( firstCategory, secondCategory, thirdCategory, relation, keyword, page)
    }

}